package com.bjc.activiti.controller;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.activiti.bean.request.DeployFlowReqVO;
import com.bjc.activiti.bean.request.MyTaskReqVO;
import com.bjc.activiti.bean.response.DeployFlowVO;
import com.bjc.activiti.bean.response.MyTaskVO;
import com.bjc.activiti.service.IActivitiService;
import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.core.bean.response.EmployeeVO;
import com.bjc.loginController.BaseAction;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * activiti controller
 *
 * @author 陈皓
 * @date 2017年11月13日
 */
@RestController
@RequestMapping("api/cloud")
@Api(value = "activiti-api", tags = { "工作流相关接口" })
public class ActivitiController extends BaseAction{

	@Autowired
	private IActivitiService activitiService;

	@ApiOperation("查询部署流程")
	@GetMapping("activiti/getDeployFlow")
	public ResultVO getDeployFlow(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,DeployFlowReqVO deployFlowReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		return ResultGenerator.getSuccess(new PageInfo<DeployFlowVO>(activitiService.getDeployFlow(deployFlowReqVO)));
	}
	
	@ApiOperation("部署流程")
	@PostMapping("activiti/deployFlow")
	public ResultVO deployFlow(@RequestParam("flowName") String flowName,@RequestParam("bpmnName") String bpmnName) {
		if (activitiService.deployFlow(flowName, bpmnName)) {
			return ResultGenerator.getSuccess();
		} else {
			return ResultGenerator.getFail("部署失败");
		}
	}

	@ApiOperation("查询审批记录")
	@GetMapping("activiti/getApproveRecord")
	public ResultVO getApproveRecord(@RequestParam("instanceId") String instanceId) {
		return ResultGenerator.getSuccess(activitiService.getApproveRecord(instanceId));
	}

	@ApiOperation("查询待办任务")
	@GetMapping("activiti/getWaitDoTask")
	public ResultVO getWaitDoTask(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,MyTaskReqVO myTaskReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		EmployeeVO employeeVO = getEmployeeVO();
		myTaskReqVO.setAssignee("["+employeeVO.getWorkId()+"]"+employeeVO.getEmployeeName());
		return ResultGenerator.getSuccess(new PageInfo<MyTaskVO>(activitiService.getWaitDoTask(myTaskReqVO)));
	}
	
	@ApiOperation("查询已办任务")
	@GetMapping("activiti/getAlreadyDoTask")
	public ResultVO getAlreadyDoTask(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,MyTaskReqVO myTaskReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		EmployeeVO employeeVO = getEmployeeVO();
		myTaskReqVO.setAssignee("["+employeeVO.getWorkId()+"]"+employeeVO.getEmployeeName());
		return ResultGenerator.getSuccess(new PageInfo<MyTaskVO>(activitiService.getAlreadyDoTask(myTaskReqVO)));
	}

	@ApiOperation("送批")
	@PostMapping("activiti/sendFlow")
	public ResultVO sendFlow(@RequestParam("flowKey") String flowKey, @RequestParam("businessId") String businessId,Map<String, Object> map) {
		String status = activitiService.sendFlow(flowKey, businessId, map);
		return ResultGenerator.getSuccess(status);
	}

	@ApiOperation("通过")
	@PostMapping("activiti/passFlow")
	public ResultVO passFlow(@RequestParam("instanceId") String instanceId,@RequestParam("taskId") String taskId,@RequestParam("comment") String comment) {
		String status = activitiService.passFlow(instanceId, taskId, comment);
		return ResultGenerator.getSuccess(status);
	}

	@ApiOperation("查询驳回步骤")
	@GetMapping("activiti/getBackStep")
	public ResultVO getBackStep(@RequestParam("instanceId") String instanceId) {
		return ResultGenerator.getSuccess(activitiService.getBackStep(instanceId));
	}

	@ApiOperation("驳回")
	@PostMapping("activiti/backFlow")
	public ResultVO backFlow(@RequestParam("instanceId") String instanceId,@RequestParam("taskId") String taskId,@RequestParam("stepId") String stepId,@RequestParam("comment") String comment) {
		return ResultGenerator.getSuccess(activitiService.backFlow(instanceId, taskId, stepId, comment));
	}

	@ApiOperation("终止")
	@PostMapping("activiti/overFlow")
	public ResultVO overFlow(@RequestParam("instanceId") String instanceId,@RequestParam("taskId") String taskId) {
		return ResultGenerator.getSuccess(activitiService.overFlow(instanceId,taskId));
	}
	
	@ApiOperation("获取instanceId")
	@GetMapping("activiti/getInstanceId")
	public ResultVO getInstanceId(@RequestParam("flowKey") String flowKey,@RequestParam("businessId") String businessId) {
		return ResultGenerator.getSuccess(activitiService.getInstanceId(flowKey, businessId));
	}
	
	@ApiOperation("转审")
	@PostMapping("activiti/transferApprove")
	public ResultVO transferApprove(@RequestParam("taskId") String taskId,@RequestParam("toUser") String toUser) {
		activitiService.transferApprove(taskId,toUser);
		return ResultGenerator.getSuccess();
	}
	
	@ApiOperation("挂起")
	@PostMapping("activiti/suspendFlow")
	public ResultVO suspendFlow(@RequestParam("instanceId") String instanceId) {
		activitiService.suspendFlow(instanceId);
		return ResultGenerator.getSuccess();
	}
	
	@ApiOperation("激活")
	@PostMapping("activiti/activateFlow")
	public ResultVO activateFlow(@RequestParam("instanceId") String instanceId) {
		activitiService.activateFlow(instanceId);
		return ResultGenerator.getSuccess();
	}
	
}
